package com.ht.medicaltreatment.viewmodel

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.ToastUtils
import com.ht.medicaltreatment.api.RetrofitUtils
import com.ht.medicaltreatment.bean.CentrifugeSampleListBean
import com.ht.medicaltreatment.bean.LoginBean
import com.ht.medicaltreatment.bean.ProjectBean
import com.xgst.driverlesscars.api.ApiResponse
import com.xgst.driverlesscars.api.ApiService
import com.xgst.lib.http.ApiResult
import kotlinx.coroutines.launch

class ReturnViewModel:BaseViewModel() {
    //根据名称模糊查询样本
    fun findSampleByName(projectId:String,sampleName:String,onSuccess:(MutableList<CentrifugeSampleListBean>)->Unit
                         , onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.findSampleByNameReturn(projectId,sampleName)
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data!!)
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    //项目
    fun projects(onResult:(MutableList<ProjectBean>)->Unit){
        viewModelScope.launch {
            val bean = RetrofitUtils.initRetrofit(ApiService::class.java).projects()
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error ->  ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    //把样本存入冰箱
    fun returnRefrigerator(returnPerson: String,returnRemarks: String,returnRfidSampleIdList: String
                           ,returnTableName: String, onSuccess:()->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.returnRefrigerator(returnRfidSampleIdList,returnPerson,returnRemarks,returnTableName)
            when(bean){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    fun getUserName(name:String,psw:String?,onSuccess:(String?)->Unit){
        viewModelScope.launch {
            var bean: ApiResult<ApiResponse<LoginBean>>?
            if(psw == null){
                bean = mApi.getUserNameForUID(name)
            }else{
                bean = mApi.getUserNameForAccount(name,psw)
            }
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data?.name)
                is ApiResult.Error -> ToastUtils.showShort(bean.errorMessage)
            }
        }
    }
}